package com.gitease.quickhub.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gitease.quickhub.model.GiteeUserCode;
import com.gitease.quickhub.model.User;
import com.gitease.quickhub.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/auth")
public class AuthController {

    private final UserService userService;
    private  final ObjectMapper objectMapper = new ObjectMapper();

    @Value("${gitee.client.id}")
    private String clientId;

    @Value("${gitee.client.secret}")
    private String clientSecret;

    @Value("${gitee.authorize.url}")
    private String authorizeUrl;

    @Value("${gitee.redirect.uri}")
    private String redirectUri;

    @Autowired
    public AuthController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/login")
    public ResponseEntity<Map<String, String>> getLoginUrl() {
        // https://gitee.com/oauth/authorize?client_id=71539a89b4085d11b274a0e9324ebb31fc2a417e9e2f832fb378684edebe95c0&redirect_uri=1bc516ec9150c76ac961706e237ec15a786240d7a39919047a98af64e48624e2&response_type=code&redirect_uri=http://localhost:3000/callback
        // http://localhost:3000/callback
        String loginUrl = authorizeUrl + "?client_id=" + clientId + "&redirect_uri=" + redirectUri + "&response_type=code";
        Map<String, String> response = new HashMap<>();
        response.put("loginUrl", loginUrl);
        return ResponseEntity.ok(response);
    }

    @PostMapping("/callback")
    public ResponseEntity<User> handleCallback(@RequestBody GiteeUserCode giteeUserCode) throws Exception {
        try {
            String code = giteeUserCode.getCode();
            User user = userService.authenticateUser(code);
            return ResponseEntity.ok(user);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(null);
        }
    }

    @PostMapping("/refresh")
    public ResponseEntity<User> refreshToken(@RequestParam("refresh_token") String refreshToken) {
        try {
            User user = userService.refreshUserToken(refreshToken);
            return ResponseEntity.ok(user);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(null);
        }
    }

    @GetMapping("/user")
    public ResponseEntity<User> getCurrentUser(@RequestHeader("Authorization") String authHeader) {
        try {
            String accessToken = authHeader.replace("Bearer ", "");
            User user = userService.getUserByAccessToken(accessToken);
            return ResponseEntity.ok(user);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(null);
        }
    }
}